package XynLeen.GraduationProject.Login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import XynLeen.GraduationProject.Activities.Activity_Register_Edit;
import XynLeen.GraduationProject.R;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,TextWatcher {

    private EditText mEtRegisterUserphone;
    private EditText mEtRegisterCallSms;
    private TextView mTvRegisterCallSms;
    private Button mBtRegisterSubmit;
    private ImageButton mIbNavigationBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    private void initView(){
        mEtRegisterUserphone=findViewById(R.id.et_register_userphone);
        mEtRegisterCallSms=findViewById(R.id.et_register_call_sms);
        mTvRegisterCallSms=findViewById(R.id.tv_register_call_sms);
        mBtRegisterSubmit=findViewById(R.id.bt_register_submit);
        mIbNavigationBack=findViewById(R.id.ib_navigation_back);

        //监听点击事件
        mIbNavigationBack.setOnClickListener(this);
        mBtRegisterSubmit.setOnClickListener(this);

        //设置控件状态
        mBtRegisterSubmit.setEnabled(false);
        mTvRegisterCallSms.setVisibility(View.INVISIBLE);

        //监听输入框文本变化
        mEtRegisterUserphone.addTextChangedListener(this);
        mEtRegisterCallSms.addTextChangedListener(this);

    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.bt_register_submit:
                startActivity(new Intent(RegisterActivity.this,Activity_Register_Edit.class));
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String userphone = mEtRegisterUserphone.getText().toString().trim();
        String callsms = mEtRegisterCallSms.getText().toString().trim();

        //是否显示获取验证码按钮
        if (!TextUtils.isEmpty(userphone)) {
            mTvRegisterCallSms.setVisibility(View.VISIBLE);
        } else {
            mTvRegisterCallSms.setVisibility(View.INVISIBLE);
        }

        //登录按钮是否可用
        if (!TextUtils.isEmpty(userphone) && !TextUtils.isEmpty(callsms)) {
            mBtRegisterSubmit.setBackgroundResource(R.drawable.login_submit);
            mBtRegisterSubmit.setTextColor(getResources().getColor(R.color.white));
            mBtRegisterSubmit.setEnabled(true);
        } else {
            mBtRegisterSubmit.setBackgroundResource(R.drawable.login_submit_lock);
            mBtRegisterSubmit.setTextColor(getResources().getColor(R.color.account_lock_font_color));
            mBtRegisterSubmit.setEnabled(false);
        }
    }

    //手机号验证码输入事件
    @Override
    public void afterTextChanged(Editable s) {

    }
}
